package com.apexsoft.front.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.utils.annotation.JsonBeanField;

/**
 * json工具
 * @author dinglei
 *
 */
public final class JsonUtil {
	
	/**
	 * json字符串转成map
	 * @param jsonStr
	 * @return
	 */
	public static Map<String,String> jsonstr2map(String jsonStr){
		Map<String,String> map = new HashMap<String,String>();
		JSONObject json = JSON.parseObject(jsonStr);
		Iterator<String> it = json.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			Object value = json.get(key);
			if(value!=null)
				map.put(key, value.toString());
			key = null;
			value = null;
		}
		it = null;
		json = null;
		return map;
	}
	
	/**
	 * javabean转换成json对象（根据字段的注解）
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public static JSONObject bean2jsonWithAnnotation(Object object) throws Exception{
		JSONObject result = new JSONObject();
		Field[] fields =  object.getClass().getDeclaredFields();
		for(Field field:fields){
			if(field.isAnnotationPresent(JsonBeanField.class)){
				String fieldName = field.getName();
				String getMethodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
				Method getMethod = object.getClass().getMethod(getMethodName, new Class[]{});
				result.put(fieldName,getMethod.invoke(object,new Object[]{}));
				fieldName = null;
				getMethodName = null;
				getMethod = null;
			}
			field = null;
		}
		fields = null;
		return result;
	}
	
	/**
	 * json对象转换成javabean(根据字典的注解)
	 * @param jsonObj
	 * @param clsType
	 * @return
	 * @throws Exception
	 */
	public static Object json2beanWithAnnotation(JSONObject jsonObj,Class<?> clsType) throws Exception{
		Object bean = Class.forName(clsType.getName()).newInstance();
		Field[] fields =  clsType.getDeclaredFields();
		for(Field field:fields){
			if(field.isAnnotationPresent(JsonBeanField.class)){
				String fieldName = field.getName();
				String setMethodName = "set" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
				Method setMethod = clsType.getMethod(setMethodName, new Class[]{field.getType()});
				if(field.getType()==int.class){
					setMethod.invoke(bean, new Object[]{jsonObj.getIntValue(fieldName)});
				}else if(field.getType()==String.class){
					setMethod.invoke(bean, new Object[]{jsonObj.getString(fieldName)});
				}else if(field.getType()==boolean.class){
					setMethod.invoke(bean, new Object[]{jsonObj.getBooleanValue(fieldName)});
				}else if(field.getType()==float.class){
					setMethod.invoke(bean, new Object[]{jsonObj.getFloatValue(fieldName)});
				}else if(field.getType()==long.class){
					setMethod.invoke(bean, new Object[]{jsonObj.getLongValue(fieldName)});
				}else if(field.getType()==double.class){
					setMethod.invoke(bean, new Object[]{jsonObj.getDoubleValue(fieldName)});
				}else{
					setMethod.invoke(bean, new Object[]{jsonObj.getString(fieldName)});
				}
				fieldName = null;
				setMethodName = null;
				setMethod = null;
			}
			field = null;
		}
		fields = null;
		return bean;
	}
	
	/**
	 * Map转换成json对象
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static JSONObject map2json(Map<?,?> map){
		JSONObject result = new JSONObject();
		Object[] keys = map.keySet().toArray();
		for(int i=0;i<keys.length;i++){
			String key = (String)keys[i];
			Object value = map.get(key);
			result.put(key, value);
			key = null;
			value = null;
		}
		keys = null;
		return result;
	}
	
	public static String optString(JSONObject pojo,String key,String defaultValue){
		if(pojo.containsKey(key))
			return pojo.getString(key);
		else
			return defaultValue;
	}
	
	public static int optInt(JSONObject pojo,String key,int defaultValue){
		if(pojo.containsKey(key))
			return pojo.getIntValue(key);
		else
			return defaultValue;
	}
	
	public static Object optObject(JSONObject pojo,String key,Object defaultValue){
		if(pojo.containsKey(key))
			return pojo.getObject(key, Object.class);
		else
			return defaultValue;
	}
	
	public static float optFloat(JSONObject pojo,String key,float defaultValue){
		if(pojo.containsKey(key))
			return pojo.getFloatValue(key);
		else
			return defaultValue;
	}
	
	public static double optDouble(JSONObject pojo,String key,double defaultValue){
		if(pojo.containsKey(key))
			return pojo.getDoubleValue(key);
		else
			return defaultValue;
	}
	
	public static boolean optBoolean(JSONObject pojo,String key,boolean defaultValue){
		if(pojo.containsKey(key))
			return pojo.getBooleanValue(key);
		else
			return defaultValue;
	}
	
	public static long optLong(JSONObject pojo,String key,long defaultValue){
		if(pojo.containsKey(key))
			return pojo.getLongValue(key);
		else
			return defaultValue;
	}
}
